package com.hzdsn.mqtt;

import java.security.cert.CertificateException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * 
 * @Title: MqttSSLSocketFactory.java
 * @description:MQTTSSL证书工厂类
 * @author: WQ
 * @date: 2019年9月3日
 */
public class MqttSSLSocketFactory {
	
	private MqttSSLSocketFactory() {
		
	}
	
	private static final TrustManager TRUST_ALL_MANAGER = new X509TrustManager() {
		@Override
		public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
				throws CertificateException {
		}

		@Override
		public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
				throws CertificateException {
		}

		@Override
		public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			return new java.security.cert.X509Certificate[0];

		}

	};

	/**
	 * 创建SSLSocketFactory
	 * @return
	 * @throws Exception
	 */
	public static SSLSocketFactory getSSLSocketFactory() throws Exception {
		SSLContext sslContext = SSLContext.getInstance("TLS");
		sslContext.init(null, new TrustManager[] { TRUST_ALL_MANAGER }, null);
		return sslContext.getSocketFactory();

	}

}